package test

import (
	"context"
	"fmt"
	"sensor/models"
	"time"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func TestFindOne() {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	client, err := mongo.Connect(ctx, options.Client().SetAuth((options.Credential{
		Username: "admin",
		Password: "123456",
	})).ApplyURI("mongodb://124.223.62.14:27017"))
	if err != nil {
		fmt.Println("出错了")
	}
	db := client.Database("sensor")
	sensor_data := new(models.SensorData)
	err = db.Collection("data").FindOne(context.Background(), bson.D{}).Decode(sensor_data)
	if err != nil {
		fmt.Println("出错了")
	}
	fmt.Println("sensor_data ===> ", sensor_data)
}
